package adventure.placeable.trigger.results;

import adventure.commands.skills.GenericSkill;
import adventure.commands.skills.Skill;
import adventure.placeable.Item;
import adventure.placeable.ItemImp;
import adventure.placeable.Mobile;
import adventure.placeable.MobileImp;
import adventure.placeable.trigger.Trigger;
import adventure.placeable.trigger.TriggerImp;
import player.Player;
import adventure.room.RoomManager;
import colour.ColourUtil;
import server.util.Broadcaster;

/**
 *
 * @author Michael Hanns
 *
 */
public class ResultString extends TriggerResult implements Cloneable {

    Item i;
    Mobile m;
    Trigger t;
    Skill s;

    public ResultString(String result, Item i, Mobile m, Trigger t, Skill s) {
        super(result);
        this.i = i;
        this.m = m;
        this.t = t;
        this.s = s;
    }

    public ResultString(String result, int uses, Item i, Mobile m, Trigger t, Skill s) {
        super(result, uses);
        this.i = i;
        this.m = m;
        this.t = t;
        this.s = s;
    }

    public ResultString() {
        super();
        this.i = new ItemImp();
        this.m = new MobileImp();
        this.t = new TriggerImp();
        this.s = null;
    }

    public void setItem(Item i) {
        this.i = i;
    }

    public void setMobile(Mobile m) {
        this.m = m;
    }

    public void setTrigger(Trigger t) {
        this.t = t;
    }

    public void setSkill(Skill s) {
        this.s = s;
    }

    public int getItemID() {
        return i.getID();
    }

    public int getMobileID() {
        return m.getID();
    }

    public int getTriggerID() {
        return t.getID();
    }

    public int getSkillID() {
        if (s != null) {
            return s.getID();
        } else {
            return 0;
        }
    }

    @Override
    public String getDescription() {
        if (super.limitedUses()) {
            String desc = super.resultText + ", uses: " + super.usesRemaining();
            return desc;
        } else {
            String desc = super.resultText;
            return desc;
        }
    }

    @Override
    public String resultImplementation(RoomManager r, Player p, String i, boolean previousFailed, Broadcaster out) {
        if (allConditionsMet(r, p, i, previousFailed)) {
            useOnce();

            String output = replaceKeywords(p);

            out.broadcastLocalToPlayer(p, replaceYou(output, p, true));

            return replaceYou(output, p, false);
        } else {
            return "";
        }
    }

    @Override
    public String getType() {
        return "STRING";
    }

    @Override
    public String replaceKeywords(Player p) {
        String out = resultText
                .replace("[PLAYER]", ColourUtil.colourise(p.getName(), ColourUtil.player)
                .replace("[ITEM]", ColourUtil.colourise(i.getName(), ColourUtil.item))
                .replace("[MOBILE]", ColourUtil.colourise(m.getName(), ColourUtil.mobile))
                .replace("[TRIGGER]", ColourUtil.colourise(t.getName(), ColourUtil.trigger))
                .replace("[USES]", ColourUtil.colourise(super.usesRemaining() + "", ColourUtil.value)));
        if(s != null) {
            out = out
                .replace("[SKILL]", ColourUtil.colourise(s.getName(), s.getTypeColour()));
        }
        return out;
    }
}